package com.study.util;

import java.util.Set;

import redis.clients.jedis.Jedis;

public class RedisSet {

	public static void main(String[] args) {
		Jedis jedis = RedisUtil.getJedis();
		
		//想集合set中添加a,b,c,d,e,f
		jedis.sadd("set", "a","b","c","d","e","f");
		//获取set集合元素
		Set<String> valueList = jedis.smembers("set");
		System.out.print("set集合元素:");
		for (String value : valueList) {
			System.out.print(value);
		}
		System.out.println();
		
		//获取set集合的成员数量
		long length = jedis.scard("set");
		System.out.println("set集合的成员数量:"+length);
		
		//判断字符串b和g是否存在集合set中
		boolean bflag = jedis.sismember("set", "b");
		System.out.println("b是否存在集合set中:" + bflag);
		boolean gflag = jedis.sismember("set", "g");
		System.out.println("g是否存在集合set中:" + gflag);
		
		//移除集合set中的元素a b
		jedis.srem("set", "a","b");
		valueList = jedis.smembers("set");
		System.out.print("移除后set集合元素:");
		for (String value : valueList) {
			System.out.print(value);
		}
		System.out.println();
		
		//随机移除集合set的一个元素并返回
		String popValue = jedis.spop("set");
		System.out.println("随机移除的元素为：" + popValue);
		valueList = jedis.smembers("set");
		System.out.print("随机移除后set集合元素:");
		for (String value : valueList) {
			System.out.print(value);
		}
		System.out.println();
		
		//向集合set1中添加d i j k
		jedis.sadd("set1", "d","i","j","k");
		//获取set集合与set1集合的差集
		Set<String> diffSet =jedis.sdiff("set","set1");
		System.out.println("set集合与set1集合的差集：");
		for (String value : diffSet) {
			System.out.print(value);
		}
		System.out.println();
		
		//获取set集合与set1集合的并集
		Set<String> unionSet =jedis.sunion("set","set1");
		System.out.println("set集合与set1集合的并集：");
		for (String value : unionSet) {
			System.out.print(value);
		}
		System.out.println();
		
		//获取set集合与set1集合的交集
		Set<String> interSet =jedis.sinter("set","set1");
		System.out.println("set集合与set1集合的交集：");
		for (String value : interSet) {
			System.out.print(value);
		}
		System.out.println();
		
		jedis.close();
	}
}
